/*
 * Copyright (C) 2009 Otávio Scherer Garcia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.technorati4j.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom.Element;

import com.googlecode.technorati4j.util.Constants;
import com.googlecode.technorati4j.util.StringUtils;

/**
 * The weblog information.
 * 
 * @author Otávio Scherer Garcia
 * @version $Revision$
 */
public final class Weblog {

    /**
     * The blog name.
     */
    private String name;

    /**
     * The blog URL.
     */
    private String url;

    /**
     * Blog RSS URL.
     */
    private String rssUrl;

    /**
     * Blog ATOM URL.
     */
    private String atomUrl;

    /**
     * The number of unbound blogs.
     */
    private Integer inboundBlogs;

    /**
     * The number of unbound links.
     */
    private Integer inboundLinks;

    /**
     * Date blog was last update.
     */
    private Date lastUpdate;

    /**
     * Cosmos ranking.
     */
    private Integer ranking;

    /**
     * The blog language (ISO 639-1 two character language code). See
     * http://www.loc.gov/standards/iso639-2/php/English_list.php for more information.
     */
    private String lang;

    /**
     * Geographical latitude.
     */
    private Double latitude;

    /**
     * Geographical longitude.
     */
    private Double longitude;

    /**
     * The author for this blog.
     */
    private List<Author> authors;

    @SuppressWarnings("unchecked")
    public Weblog(Element element) {
        name = element.getChildText("name");
        url = element.getChildText("url");
        rssUrl = element.getChildText("rssurl");
        atomUrl = element.getChildText("atomurl");
        inboundBlogs = StringUtils.toInteger(element.getChildText("inboundblogs"));
        inboundLinks = StringUtils.toInteger(element.getChildText("inboundlinks"));
        lastUpdate = StringUtils.toDate(element.getChildText("lastupdate"), Constants.DATE_FULL);
        ranking = StringUtils.toInteger(element.getChildText("rank"));
        lang = element.getChildText("lang");
        latitude = StringUtils.toDouble(element.getChildText("lat"));
        longitude = StringUtils.toDouble(element.getChildText("lon"));

        authors = new ArrayList<Author>();
        if (element.getChild("authors") != null) {
            List<Element> _items = element.getChild("authors").getChildren("author");
            for (Element item : _items) {
                this.authors.add(new Author(item));
            }
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getRssUrl() {
        return rssUrl;
    }

    public void setRssUrl(String rssUrl) {
        this.rssUrl = rssUrl;
    }

    public String getAtomUrl() {
        return atomUrl;
    }

    public void setAtomUrl(String atomUrl) {
        this.atomUrl = atomUrl;
    }

    public Integer getInboundBlogs() {
        return inboundBlogs;
    }

    public void setInboundBlogs(Integer inboundBlogs) {
        this.inboundBlogs = inboundBlogs;
    }

    public Integer getInboundLinks() {
        return inboundLinks;
    }

    public void setInboundLinks(Integer inboundLinks) {
        this.inboundLinks = inboundLinks;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public Integer getRanking() {
        return ranking;
    }

    public void setRanking(Integer ranking) {
        this.ranking = ranking;
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public List<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(List<Author> authors) {
        this.authors = authors;
    }

    @Override
    public String toString() {
        final StringBuilder str = new StringBuilder();
        str.append("(name=");
        str.append(name);
        str.append("&url=");
        str.append(url);
        str.append("&rssUrl=");
        str.append(rssUrl);
        str.append("&atomUrl=");
        str.append(atomUrl);
        str.append("&inboundBlogs=");
        str.append(inboundBlogs);
        str.append("&inboundLinks=");
        str.append(inboundLinks);
        str.append("&lastUpdate=");
        str.append(lastUpdate);
        str.append("&ranking=");
        str.append(ranking);
        str.append("&lang=");
        str.append(lang);
        str.append("&latitude=");
        str.append(latitude);
        str.append("&longitude=");
        str.append(longitude);
        str.append("&authors=");
        str.append(authors);
        str.append(")");

        return str.toString();
    }

}
